﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GIAODIEN
{
    public partial class frmthemsach : Form
    {
        public frmthemsach()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void loadcbotg()
        {
            DataTable dstg = new DataTable();
            dstg = BUS_TacGia.laytacgia();
            cbotg.DataSource = dstg;
            cbotg.DisplayMember = "tentg";
            cbotg.ValueMember = "matg";
            cbotg.SelectedIndex = -1;
            cbotg.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbotg.AutoCompleteSource = AutoCompleteSource.ListItems;
        }
        void loadcboloaisach()
        {
            DataSet dsls = new DataSet();
            dsls = BUS_LoaiSach.layloaisach();
            cbotheloai.DataSource = dsls.Tables[0];
            cbotheloai.DisplayMember = "tenloaisach";
            cbotheloai.ValueMember = "maloaisach";
            cbotheloai.SelectedIndex = -1;
            cbotheloai.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbotheloai.AutoCompleteSource = AutoCompleteSource.ListItems;
        }
        void formload()
        {
            hienthilistview();
            loadcbotg();
            loadcboloaisach();
            txtngaynhap.Text = DateTime.Parse(System.DateTime.Now.ToString()).ToShortDateString();
            txtnguoinhap.Text = bientoancuc.ttnhanvien.TenNhanVien;
        }
        private void frmthemsach_Load(object sender, EventArgs e)
        {
            formload();
            anhienbtn(false);
            bientoancuc.sach = 1;
        }

        DataSet ds = new DataSet();
        void hienthilistview()
        {
            lvhienthi.Items.Clear();
            DataTable dssach = new DataTable();
            dssach = BUS_Sach.laysach();
            ListViewItem it = new ListViewItem();
            foreach (DataRow dr in dssach.Rows)
            {
                it = lvhienthi.Items.Add(dr["masach"].ToString());
                for (int i = 1; i < dssach.Columns.Count; i++)
                {
                    
                    if (i == 4)
                        it.SubItems.Add(DateTime.Parse(dr[i].ToString()).ToShortDateString());
                    else
                        it.SubItems.Add(dr[i].ToString());
                }


            }
        }

        void anhienbtn(Boolean b)
        {
            btnxoa.Enabled = b;
            btnsua.Enabled = b;
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnthemtg_Click(object sender, EventArgs e)
        {
            DTO_tacgia tg = new DTO_tacgia();
            tg.Tentg = cbotg.Text;
            if (BUS_TacGia.themtacgia(tg))
            {
                MessageBox.Show("Thêm thành công", "thông báo");
                loadcbotg();
                
            }
        }
        
        
        private void btnthem_Click(object sender, EventArgs e)
        {
            DTO_Sach s = new DTO_Sach();
            s.MaLoaiSach = cbotheloai.SelectedValue.ToString();
            s.TenSach = txttensach.Text;
            s.Gia = txtgia.Text;
            s.TacGia = cbotg.SelectedValue.ToString();
            s.NgayNhap = txtngaynhap.Text;
            s.ManNV = bientoancuc.ttnhanvien.MaNhanVien.ToString();
            if (BUS_Sach.themsach(s))
            {
                MessageBox.Show("Thêm thành công", "Thông báo");
                hienthilistview();
            }
        }

        private void cbotg_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnthemloai_Click(object sender, EventArgs e)
        {
      
                DTO_LoaiSach loaisach = new DTO_LoaiSach();
                loaisach.TenLoaiSach = cbotheloai.Text;
                if (BUS_LoaiSach.themloaisach(loaisach))
                {
                    MessageBox.Show("Thêm thành công", "thông báo");
                    loadcboloaisach();
                    
                }
            
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DTO_Sach s = new DTO_Sach();
            s.Masach = txtmasach.Text;
            DialogResult tb = new DialogResult();
            tb = MessageBox.Show("Bạn có muốn xóa", "thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (tb == DialogResult.Yes)
                if (BUS_Sach.xoasach(s))
                {
                    MessageBox.Show("Xóa thành công", "thông báo");
                    hienthilistview();
                    anhienbtn(false);
                    formload();
                    lammmoi();

                }
            
        }
        void lammmoi()
        {
            txtmasach.Text = "";
            txttensach.Text = "";
            txtgia.Text = "";
            
        }
        private void lvhienthi_MouseDown(object sender, MouseEventArgs e)
        {
            anhienbtn(true);
            ListViewItem it = new ListViewItem();
            it = lvhienthi.GetItemAt(e.X, e.Y);
            if (it != null)
            {
                txtmasach.Text = it.SubItems[0].Text;
                txttensach.Text = it.SubItems[1].Text;
                cbotg.Text = it.SubItems[2].Text;
                cbotheloai.Text = it.SubItems[6].Text;
                txtngaynhap.Text = it.SubItems[4].Text;
                txtgia.Text = it.SubItems[3].Text;
                txtnguoinhap.Text = it.SubItems[5].Text;
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DTO_Sach s = new DTO_Sach();
            s.Masach = txtmasach.Text;
            s.MaLoaiSach = cbotheloai.SelectedValue.ToString();
            s.TenSach = txttensach.Text;
            s.Gia = txtgia.Text;
            s.TacGia = cbotg.SelectedValue.ToString();
            s.NgayNhap = txtngaynhap.Text;
            s.ManNV = bientoancuc.ttnhanvien.MaNhanVien.ToString();
            if (BUS_Sach.suasach(s))
            {
                MessageBox.Show("Sửa thành công", "thông báo");
                hienthilistview();
                anhienbtn(false);
                formload();
                lammmoi();

            }
        }

        private void frmthemsach_FormClosing(object sender, FormClosingEventArgs e)
        {
            bientoancuc.sach = 0;
        }
    }
}
